<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/7/23
 * Time: 11:53
 */

namespace app\common\model;

use app\backstage\model\ManagerUser;
use think\Model;

class Novel extends Model
{
    use Common;

    protected $table = 'novel';


    /**
     * 频道
     * @return \think\model\relation\HasOne
     * @Author: whh
     */
    public function freq()
    {
        return $this->hasOne(NovelFreq::class, 'id', 'freq_id')->field('id,name');
    }


    /**
     * 分类
     * @return \think\model\relation\HasOne
     * @Author: whh
     */
    public function novelClass()
    {
        return $this->hasOne(NovelClass::class, 'id', 'class_id')->field('id,name');
    }

    /**
     * 子分类
     * @return \think\model\relation\HasOne
     * @Author: whh
     */
    public function novelClassSon()
    {
        return $this->hasOne(NovelClassSon::class, 'id', 'son_class_id')->field('id,name');
    }


    /**
     * 标签
     * @return \think\model\relation\BelongsToMany
     */
    public function labels()
    {
        return $this->belongsToMany(NovelLabel::class, NovelToLabelAccess::class, 'label_id', 'novel_id');
    }


    /**
     * 标签
     * @return \think\model\relation\BelongsToMany
     */
    public function agents()
    {
        return $this->belongsToMany(ManagerUser::class, NovelToAgentAccess::class, 'agent_id', 'nid');
    }


    /**
     * 简介格式化
     * @param $value
     * @param $data
     * @return string
     * @Author: whh
     */
    public function getBriefTextAttr($value, $data)
    {
        return trim(str_ireplace("\r\n", "<br/>", $data['brief']));
    }


    /**
     * 字数格式化
     * @param $value
     * @param $data
     * @return string
     * @Author: whh
     */
    public function getNumberWordsTextAttr($value, $data)
    {
        if ($data['number_words'] > 100000000) {
            $data['number_words'] = round($data['number_words'] / 100000000, 1) . '亿字';
        } elseif ($data['number_words'] > 10000000) {
            $data['number_words'] = round($data['number_words'] / 10000000, 1) . '千万字';
        } elseif ($data['number_words'] > 10000) {
            $data['number_words'] = round($data['number_words'] / 10000, 1) . '万字';
        } elseif ($data['number_words'] > 1000) {
            $data['number_words'] = round($data['number_words'] / 1000, 1) . '千字';
        } else {
            $data['number_words'] = $data['number_words'] . '字';
        }
        return $data['number_words'];
    }

    /**
     * 连载状态
     * @param $value
     * @param $data
     * @return string
     * @Author: whh
     */
    public function getUpdateStatusTextAttr($value, $data)
    {
        $status = [0 => '连载', 1 => '完结', 2 => '弃更'];
        return $status[$data['update_status']];
    }


    /**
     * 上架状态
     * @param $value
     * @param $data
     * @return string
     * @Author: whh
     */
    public function getShelfTextAttr($value, $data)
    {
        $status = [0 => '未上架', 1 => '上架'];
        return $status[$data['in_shelf']];
    }

    /**
     * 封面处理
     * @param $value
     * @param $data
     * @return string
     * @Author: whh
     */
    public function getCoverTextAttr($value, $data)
    {
        return config('img_domain') . $data['cover'];
    }

    /**
     * 可选择的子分类
     * @return NovelClassSon[]|\think\Collection
     */
    public function optionalSonClass()
    {
        return NovelClassSon::where("freq_id", $this->freq_id)->where("class_id", $this->class_id)->select();
    }
}